package com.example.lxj.sources.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.example.lxj.sources.entity.TbDevice;
import com.example.lxj.sources.entity.TbMaintenanceRecords;
import com.example.lxj.sources.mapper.TbDeviceMapper;
import com.example.lxj.sources.mapper.TbMaintenanceRecordsMapper;
import com.example.lxj.sources.service.ITbMaintenanceRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备维护Service业务层处理
 *
 * @author lxj
 * @date 2024-10-15
 */
@Service
public class TbMaintenanceRecordsServiceImpl implements ITbMaintenanceRecordsService {
    @Autowired
    private TbMaintenanceRecordsMapper tbMaintenanceRecordsMapper;

    @Autowired
    private TbDeviceMapper tbDeviceMapper;

    /**
     * 查询设备维护
     *
     * @param id 设备维护主键
     * @return 设备维护
     */
    @Override
    public TbMaintenanceRecords selectTbMaintenanceRecordsById(Long id) {
        return tbMaintenanceRecordsMapper.selectTbMaintenanceRecordsById(id);
    }

    /**
     * 查询设备维护列表
     *
     * @param tbMaintenanceRecords 设备维护
     * @return 设备维护
     */
    @Override
    public List<TbMaintenanceRecords> selectTbMaintenanceRecordsList(TbMaintenanceRecords tbMaintenanceRecords) {
        return tbMaintenanceRecordsMapper.selectTbMaintenanceRecordsList(tbMaintenanceRecords);
    }

    /**
     * 新增设备维护
     *
     * @param tbMaintenanceRecords 设备维护
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbMaintenanceRecords(TbMaintenanceRecords tbMaintenanceRecords) {
        tbMaintenanceRecords.setCreateTime(DateUtil.date());
        // 更新设备状态
        TbDevice tbDevice = new TbDevice();
        tbDevice.setId(tbMaintenanceRecords.getDeviceId());
        tbDevice.setStatus("1");
        tbDevice.setLastMaintenanceDate(tbMaintenanceRecords.getMaintenanceDate());
        int i = tbMaintenanceRecordsMapper.insertTbMaintenanceRecords(tbMaintenanceRecords);
        if(0 < i){
            return tbDeviceMapper.updateTbDevice(tbDevice);
        }else{
            return -1;
        }
    }

    /**
     * 修改设备维护
     *
     * @param tbMaintenanceRecords 设备维护
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbMaintenanceRecords(TbMaintenanceRecords tbMaintenanceRecords) {
        tbMaintenanceRecords.setUpdateTime(DateUtil.date());
        return tbMaintenanceRecordsMapper.updateTbMaintenanceRecords(tbMaintenanceRecords);
    }

    /**
     * 批量删除设备维护
     *
     * @param ids 需要删除的设备维护主键
     * @return 结果
     */
    @Override
    public int deleteTbMaintenanceRecordsByIds(Long[] ids) {
        return tbMaintenanceRecordsMapper.deleteTbMaintenanceRecordsByIds(ids);
    }

    /**
     * 删除设备维护信息
     *
     * @param id 设备维护主键
     * @return 结果
     */
    @Override
    public int deleteTbMaintenanceRecordsById(Long id) {
        return tbMaintenanceRecordsMapper.deleteTbMaintenanceRecordsById(id);
    }
}
